package com.caishi.lkx.course.api;

import com.aliyun.vod20170321.models.GetCategoriesResponseBody;
import com.aliyun.vod20170321.models.GetVideoListRequest;
import com.aliyun.vod20170321.models.GetVideoListResponse;
import com.aliyun.vod20170321.models.GetVideoListResponseBody;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.caishi.lkx.common.BaseApi;
import com.caishi.lkx.common.aliyun.AliyunVideoPluginsExt;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author lkx
 * &#064;date  2024/5/23
 * &#064;description
 */
@Slf4j
@Tag(name = "阿里云视频相关接口")
@RestController
@RequestMapping("/kc/course/aliyun/video")
public class AliyunVideoApi extends BaseApi {

    @Autowired
    private AliyunVideoPluginsExt aliyunVideoPluginsExt  ;


    @GetMapping("/cate")
    @Operation(summary = "获取阿里云分类")
    public GetCategoriesResponseBody GetCategories(@RequestParam Long cateId){
        try {
            return aliyunVideoPluginsExt.getCategories(cateId).body;
        } catch (Exception e) {
            log.error("获取分类异常",e);
        }
        return null;
    }

    @GetMapping("/page")
    @Operation(summary = "获取阿里云视频列表")
    public IPage<GetVideoListResponseBody.GetVideoListResponseBodyVideoListVideo> getAliyunVideoList(@ModelAttribute Page<GetVideoListRequest> page, @ModelAttribute GetVideoListRequest videoInfo){
        Page<GetVideoListResponseBody.GetVideoListResponseBodyVideoListVideo> videoPage = new Page<GetVideoListResponseBody.GetVideoListResponseBodyVideoListVideo>();
        try {
            videoInfo.setPageNo((int)page.getCurrent()) ;
            videoInfo.setPageSize((int)page.getSize());
            GetVideoListResponse videoListResponse = aliyunVideoPluginsExt.getVideoList(videoInfo);
            videoPage.setCurrent(page.getCurrent()) ;
            videoPage.setSize(page.getSize());
            videoPage.setTotal(videoListResponse.body.getTotal());
            videoPage.setRecords(videoListResponse.body.videoList.video);

        }catch(com.aliyun.tea.TeaException e){
            if(e.code.equals("InvalidVideo.NotFound")){
                log.error("未找到视频");
            }

        }
        catch (Exception e) {
            log.error("获取阿里云视频列表异常",e);
        }
        return videoPage;
    }






}
